//
//  IMConversationTableViewCell.m
//  MLIM
//
//  Created by molon on 15/6/24.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMConversationTableViewCell.h"
#import "IMCommon.h"

#define kGrayTextColor [UIColor colorWithWhite:0.543 alpha:1.000]

#define kCellImageSide 45.0f
@interface IMConversationTableViewCell()

@property (nonatomic, strong) MLImageView *cellImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) MLLabel *lastMessageContentLabel;

@end

@implementation IMConversationTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = IMCONFIGM.backgroundColorForIMConversationListCell;
        
        [self.contentView addSubview:self.cellImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.lastMessageContentLabel];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    if (selected) {
        [self.cellImageView setLayerBackgroundColor:IM_AvatarImageBackgroundColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted) {
        [self.cellImageView setLayerBackgroundColor:IM_AvatarImageBackgroundColor];
    }
}

#pragma mark - setter
- (void)setImConv:(IMConversation *)imConv
{
    _imConv = imConv;
    
    if (![imConv.avatarURL isEqual:self.cellImageView.sd_imageURL]||!self.cellImageView.image) {
        [self.cellImageView sd_setImageWithURL:imConv.avatarURL];
    }
    
    self.nameLabel.text = imConv.name;
    if (imConv.lastMessageTime<=0) {
        self.timeLabel.text = @"";
    }else{
        self.timeLabel.text = [[NSDate dateWithTimeIntervalSince1970:imConv.lastMessageTime]commonChineseStringWithStyle:TimeChineseStringStyleVerySimple];
    }
    id simpleDescription = [imConv.lastMessage simpleDescription];
    if ([simpleDescription isKindOfClass:[NSAttributedString class]]) {
        self.lastMessageContentLabel.attributedText = simpleDescription;
    }else{
        self.lastMessageContentLabel.text = simpleDescription;
    }
    [self setNeedsLayout];
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frameWidth;
    CGFloat height = self.contentView.frameHeight;
    
#define kPadding 10.0f
    //头像
    self.cellImageView.frame = CGRectMake(kPadding, (height-kCellImageSide)/2, kCellImageSide, kCellImageSide);
    CGSize timeLabelSize = [self.timeLabel.text singleSizeWithMaxWidth:width-kPadding-(self.cellImageView.frameRight+kPadding) withFont:self.timeLabel.font];
    
    self.timeLabel.frame = CGRectMake(width-kPadding-timeLabelSize.width, self.cellImageView.frameY, timeLabelSize.width, timeLabelSize.height);
    
#define kYOffset 2.0f
    CGSize nameLabelSize = [self.nameLabel.text singleSizeWithMaxWidth:self.timeLabel.frameX-kPadding-(self.cellImageView.frameRight+kPadding) withFont:self.nameLabel.font];
    self.nameLabel.frame = CGRectMake(self.cellImageView.frameRight+kPadding, self.cellImageView.frameY+kYOffset, nameLabelSize.width, nameLabelSize.height);
    
    CGSize lastMessageContentLabelSize = [self.lastMessageContentLabel.text singleSizeWithMaxWidth:width-kPadding-(self.nameLabel.frameX) withFont:self.lastMessageContentLabel.font];
    self.lastMessageContentLabel.frame = CGRectMake(self.nameLabel.frameX, self.cellImageView.frameBottom-lastMessageContentLabelSize.height-kYOffset, lastMessageContentLabelSize.width, lastMessageContentLabelSize.height);
    
    
    if (self.imConv.unreadCount<=0) {
        [self.cellImageView removeBadgeValue];
    }else if (self.imConv.unreadCount<=99) {
        [self.cellImageView showBadgeValue:[NSString stringWithFormat:@"%ld",self.imConv.unreadCount]];
    }else{
        [self.cellImageView showBadgeValue:@"99+"];
    }
}

#pragma mark - height
+ (CGFloat)heightForObject:(id)object maxWidth:(CGFloat)maxWidth
{
    return 65.0f;
}

#pragma mark - getter
- (MLImageView *)cellImageView
{
    if (!_cellImageView) {
        MLImageView *imageView = [[MLImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [imageView activateMLSmoothCornerWithRadius:kCellImageSide/2 andBackgroundColor:IM_AvatarImageBackgroundColor andShouldRasterize:NO];
//        imageView.clipsToBounds = YES;
        
        _cellImageView = imageView;
    }
    return _cellImageView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:16.0f];
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        
        _nameLabel = label;
    }
    return _nameLabel;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = kGrayTextColor;
        label.font = [UIFont systemFontOfSize:12.0f];
        
        _timeLabel = label;
    }
    return _timeLabel;
}

- (MLLabel *)lastMessageContentLabel
{
    if (!_lastMessageContentLabel) {
        MLLabel *label = [[MLLabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = kGrayTextColor;
        label.font = [UIFont systemFontOfSize:14.0f];
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        
        _lastMessageContentLabel = label;
    }
    return _lastMessageContentLabel;
}

@end
